package DGWeb.Model;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.tomcat.dbcp.dbcp.BasicDataSource;

import DGWeb.AppConstants;

public class AbstractGet 
{
	public static String chkUsersTopics(String incomingText) throws NamingException, SQLException
	{
		String OutText = "";
		String[] dt = incomingText.split(" ");
		Context context = null;
		BasicDataSource ds = null;
		Connection conn = null;
		Statement stmt = null;
		ResultSet res = null;
		for( int i=0; i < dt.length ; i++)
		{
			if(dt[i].startsWith("@") ) // if it is valid User
			{
				
				try{
					if (context == null )
						context = new InitialContext();
					if( ds == null)
						ds = (BasicDataSource)context.lookup(AppConstants.DB_DATASOURCE);
					if ( conn == null)
						conn = ds.getConnection();
					if ( stmt == null)
						stmt = conn.createStatement();
					// need to check valid Nickname
					String[] tempArray = AppConstants.USERS_SELECT_FIND_USER_FROM_NICK.split("//");
					String exec = tempArray[0] + dt[i].substring(1) + tempArray[1];
					
					res = stmt.executeQuery(exec);
					
					if ( !res.next() )
					{
						OutText += dt[i]+" ";
					}
					else
					{
						String Nick = res.getString(AppConstants.NICKNAME);
						if (Nick != null) // the nickname is exist
						{
							OutText += "<a ng-click=\"btnShowMemberProfile('"+Nick+"')\" style=\"color:#005F6B;\">@"+Nick+"</a> ";
						}
						else
						{
							OutText += dt[i]+" ";
						}
					}


					
				}catch (SQLException | NamingException e) {
					try {
						if ( res!= null)
							res.close();
						if (stmt != null)
							stmt.close();
						if (context != null)
							context.close();
						if ( conn != null )
							conn.close();
							
					} catch (NamingException|SQLException e1) {
						
						e1.printStackTrace();
					}
					
				}

			}
			else if (dt[i].startsWith("#")) // if it is Topic
			{
				try{
					if (context == null )
						context = new InitialContext();
					if( ds == null)
						ds = (BasicDataSource)context.lookup(AppConstants.DB_DATASOURCE);
					if ( conn == null)
						conn = ds.getConnection();
					if ( stmt == null)
						stmt = conn.createStatement();
					
					String exec = AppConstants.GET_TOPICS_BY_TEXT.replaceFirst("//", dt[i]);
					res = stmt.executeQuery(exec);
					if ( res.next() ) // if we found the topic
					{
						OutText +="<a ng-click=\"btnTopicsLoad('"+dt[i]+"')\"style=\"color:#850691;\" >"+ dt[i] +"</a> ";
					}
					else
					{
						OutText += dt[i]+" ";
					}
					
				}catch (SQLException | NamingException e) {
					
					if ( res!= null)
						res.close();
					if (stmt != null)
						stmt.close();
					if (context != null)
						context.close();
					if ( conn != null )
						conn.close();
				}
			}
			else
			{
				OutText += dt[i]+" ";
			}

			
			
		}
		
		if ( res!= null)
			res.close();
		if (stmt != null)
			stmt.close();
		if (context != null)
			context.close();
		if ( conn != null )
			conn.close();
		
		return OutText;
	}
}
